package lev01_beginnerClass.class04_randToRand;

import java.util.Arrays;

/**
 * give a function can not be changeed as a black  box to produce a range num with equal probability
 * using this funciotn to produce a new range num equal probality
 */
public class L01_RandToRand15to17 {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        int[] count = new int[8];
        for(int i = 0;i<1000000;i++){
            count[f17()]++;
        }
        System.out.println(Arrays.toString(count));
    }

    /**
     * generate a random num from 1 -5 with equal probality
     * this function only can be userd and not be changed
     * @return
     */
    public static int f15(){
        return (int)(Math.random()*5+1);
    }

    /**
     * use f15 to make a funciton genratind  0 and 1 with euqal probablity
     *
     * @return
     */
    public static int f01fromf15(){
        int ans = 0;
        //1,2return 0    4,5 reuttn 1   3 try again
        do{
            ans = f15();

        }
        while(ans == 3);
        return ans < 3 ? 0:1;
    }

    /**
     * generate a random num from 0 - 6 with equal probablity
     * @return
     */
    public static int f06(){
        int ans = 0 ;
        do{
            ans = (f01fromf15() << 2 )+ (f01fromf15()<<1)+(f01fromf15());
        }while (ans == 7);
        return ans;
    }

    public static  int f17(){
        return f06()+1;
    }


}
